# coding=utf-8
# python3ex01'
import urllib2
import urllib
import cookielib

#user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
# values = {'username' : 'cqc',  'password' : 'XXXX' }
#headers = {'User-Agent': user_agent}

headers = {'Host': 'www.super-ping.com',
           'Connection': 'keep-alive',
           'Cache-Control': 'max-age=0',
           'Accept': 'text/html, */*; q=0.01',
           'X-Requested-With': 'XMLHttpRequest',
           'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
           'DNT': '1',
          # 'Referer': 'http://www.super-ping.com/?ping=www.google.com&locale=sc',
           'Accept-Encoding': 'gzip, deflate, sdch',
           'Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6',
           'Referer': 'http://www.cnbeta.com/articles'
           }


def hello():
    print urllib2.urlopen("http://www.baidu.com").read()


def post():
    data = {"user.account": "superoot", "user.password": "Gotterdamerung"}
    postdate = urllib.urlencode(data)
    url = "http://www.metering.danfoss.com/login"
    request = urllib2.Request(url, postdate, headers)
    # urllib2.urlopen(url,postdate,headers)
    try:
        print urllib2.urlopen(request).read()
    except urllib2.HTTPError, e:
        print e.code
        print e.reason
    except urllib2.URLError, e:
        if hasattr(e.reason):
            print e.reason
    else:
        print 'OK'


def downloadimage(url):
    urllib.urlretrieve(url, 'c://WORKSPACE//logs//a.jpg')


def initcookie():
    cookie = cookielib.CookieJar()
    cookie_file = cookielib.MozillaCookieJar('c://WORKSPACE//logs//a.txt')
    handler = urllib2.HTTPCookieProcessor(cookie_file)
    opener = urllib2.build_opener(handler)
    # data = {"user.account": "superoot", "user.password": "Gotterdamerung"}
    # postdate = urllib.urlencode(data)
    # url = "http://www.metering.danfoss.com/login"
    # request = urllib2.Request(url, postdate, headers)
    opener.open("http://www.baidu.com")
    # print urllib2.urlopen(request).read()
    for item in cookie:
        print item.name
        print item.value
    cookie_file.save(ignore_discard=True, ignore_expires=True)


def test():
    filename = 'c://WORKSPACE//logs//cookie.txt'
    # 声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
    cookie = cookielib.MozillaCookieJar(filename)
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    postdata = urllib.urlencode({
        'stuid': '201200131012',
        'pwd': '23342321'
    })
    # 登录教务系统的URL
    loginUrl = 'http://python.jobbole.com/81344/'
    # 模拟登录，并把cookie保存到变量
    result = opener.open(loginUrl)
    # 保存cookie到cookie.txt中
    cookie.save(ignore_discard=True, ignore_expires=True)
    # 利用cookie请求访问另一个网址，此网址是成绩查询网址
    # gradeUrl = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/bkscjcx.curscopre'
    # 请求访问成绩查询网址
    # result = opener.open(gradeUrl)
    # print result.read()


if __name__ == '__main__':
    # downloadimage('http://i-2.yxdown.com/2015/3/18/KDkwMHgp/fec26de9-8727-424a-b272-f2827669a320.jpg')
    test()
    print 'OK'
